package com.ilikesoup.instant.classifier;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * @ClassName ClassifierBuilder
 * @Description: 分类器建造者
 * @Author gaoxiang
 * @Date 2020/8/7 14:39
 * @Version V1.0
 **/
public class ClassifierBuilder<T> {
    private final List<Predicate<? super T>> classifiers;
    private final List<Action<List<T>, ?>> ifActions;
    private Action<List<T>, ?> elseAction;

    public ClassifierBuilder() {
        classifiers = new ArrayList<>();
        ifActions = new ArrayList<>();
    }

    public ClassifierBuilder<T> onIf(Predicate<? super T> condition, ActionBuilder<List<T>> actionBuilder) {
        classifiers.add(condition);
        ifActions.add(actionBuilder.build());
        return this;
    }

    public ClassifierBuilder<T> onIf(Predicate<? super T> condition, Action<List<T>, ?> action) {
        classifiers.add(condition);
        ifActions.add(action);
        return this;
    }

    public ClassifierBuilder<T> onElse(ActionBuilder<List<T>> actionBuilder) {
        elseAction = actionBuilder.build();
        return this;
    }

    public ClassifierBuilder<T> onElse(Action<List<T>, ?> action) {
        elseAction = action;
        return this;
    }

    public Classifier<T> build() {
        return new Classifier<>(classifiers, ifActions, elseAction);
    }

}
